package MonteCarlo;

import java.util.Random;

public class MonteCarloMethod {

	public double monteCarloMethodCalcul(String callPutFlag,double S, double X, double T, 
			double r, double b, double v, int nSteps, int nSimulations){
		 
		double dt; 
		double st;
		double sum;
		double drift; 
		double vSqrdt;
		double result;
		int i,j,z = 0;
		Random rand = new Random();
		
		dt = T / nSteps;
		drift = (b - (v*v) / 2) * dt;
		vSqrdt = v * Math.sqrt(dt);
		sum = 0;

		if ("c".equals(callPutFlag)){
			z = 1; 
		}
		else if ("p".equals(callPutFlag)){
			z = -1; 
		}
		
		for (i=1; i<=nSimulations; i++){
			st = S; 
			for (j=1; j<=nSteps; j++){
				st = st * Math.exp(drift + vSqrdt * rand.nextGaussian()); 
			}
			sum = sum + Math.max(z*(st-X), 0); 
		}
		
		result = Math.exp((-r*T)*(sum/nSimulations)); 

		return result; 
	}



	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		double S = 1470.0;
		double X = 700.0;
		double T = 5; 
		double r = 2.3;
		double b = 2.6;
		double v = 400.0;
		int nSteps = 150;
		int nSimulations = 100;
		
		MonteCarloMethod mcm = new MonteCarloMethod();
		
		System.out.println(mcm.monteCarloMethodCalcul("c", S, X, T, r, b, v, nSteps, nSimulations));
		

	}

}
